<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;
use app\admin\model\Speiales;
class Special extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $field='id,name,classify_id,cover,recommend,group,sort,status,group_ctime,group_etime,sum';
        $list=db('special')->field($field)->paginate(5);
        $data=$list->items();
        foreach($data as $key=>$vo){
            $data[$key]['group_ctime']=date('Y-m-d H:i:s',$vo['group_ctime']);
            $data[$key]['group_etime']=date('Y-m-d H:i:s',$vo['group_etime']);
            $data[$key]['classify']=db('classify')->where('id',$vo['classify_id'])->value('classname');
        }
         $this->assign("data",$data);
         $this->assign("list",$list);
         return $this->fetch();
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
       
        return $this->fetch();
    }
     public  function upload()
     {
         $file =request()->file('file');

        $info=$file->move('./public/upload');
          if ($info) {
                $path='http://erzu.com'.'/public/upload/'.$info->getSaveName();
              return json(["code"=>0,"msg"=>"上传成功","data"=>$path]);
          }else{
              return json(["code"=>1,"msg"=>"上传失败","data"=>'']);
          }
       
     }
     public function getsu()
     {
        $name=input('title');
        if($name){
            $list=db('fodder')->where('name',$name)->paginate(3);
            $data=$list->items();
            $this->assign("data",$data);
            $this->assign("list",$list);
            return $this->fetch();
        }else{
         $list=db('fodder')->paginate(3);
        $data=$list->items();
         $this->assign("data",$data);
         $this->assign("list",$list);
         return $this->fetch();
        }
     }
    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save()
    {
        $data=input();
        $data['group_ctime']=strtotime($data['group_ctime']);
        $data['group_etime']=strtotime($data['group_etime']);
        $data['sum']=mb_strlen($data['matter_id']);
        $special=new Speiales();
        $res=$special->add($data);
        if ($res){
                return $this->success('添加成功',url('Special/index'));
        }else{
                return $this->error('添加失败');
        }
    }
    public function lod()
    {
        $data=input();
        $id= implode(',',$data['data']);
        $where = [];
        $where[]= ["id","in",$id];
        $res=db('fodder')->where($where)->select();
        if($res){
            return json(["code"=>0,"data"=>$res]);
        }else{
            return json(["code"=>1,"data"=>'']);
        }
    }
    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        $data=db('special')->where('id',$id)->find();
        $data['group_ctime']=date('Y-m-d H:i:s',$data['group_ctime']);
        $data['group_etime']=date('Y-m-d H:i:s',$data['group_etime']);
        $list=db('classify')->select();
        dump($data);
        $this->assign('list',$list);
        $this->assign("data",$data);
        return $this->fetch();
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
